package com.itheima.health.service.impl;

import com.itheima.health.dao.HotSetmealDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.ReportDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.pojo.HotSetmeal;
import com.itheima.health.pojo.Report;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

/**
 * 运营数据统计
 */

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportDao reportDao;
    @Autowired
    private HotSetmealDao hotSetmealDao;
    @Autowired
    private SetMealDao setMealDao;
    @Autowired
    private OrderDao orderDao;


    @Override
    public Report getBusinessReportData() {
        Report report = new Report();
        Integer todayNewMember = reportDao.findTodayNewMember();
        Integer totalMember = reportDao.findTotalMember();
        Integer thisWeekNewMember = reportDao.findThisWeekNewMember();
        Integer thisMonthNewMember = reportDao.findThisMonthNewMember();
        Integer todayOrderNumber = reportDao.findTodayOrderNumber();
        Integer todayVisitsNumber = reportDao.findTodayVisitsNumber();
        Integer thisWeekOrderNumber = reportDao.findThisWeekOrderNumber();
        Integer thisWeekVisitsNumber = reportDao.findThisWeekVisitsNumber();
        Integer thisMonthOrderNumber = reportDao.findThisMonthOrderNumber();
        Integer thisMonthVisitsNumber = reportDao.findThisMonthVisitsNumber();
        SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
        sdf.applyPattern("yyyy-MM-dd");// a为am/pm的标记
        Date date = new Date();// 获取当前时间
        String data = sdf.format(date);
        report.setReportDate(data);
        report.setTodayNewMember(todayNewMember);
        report.setTotalMember(totalMember);
        report.setThisWeekNewMember(thisWeekNewMember);
        report.setThisMonthNewMember(thisMonthNewMember);
        report.setTodayOrderNumber(todayOrderNumber);
        report.setTodayVisitsNumber(todayVisitsNumber);
        report.setThisWeekOrderNumber(thisWeekOrderNumber);
        report.setThisWeekVisitsNumber(thisWeekVisitsNumber);
        report.setThisMonthOrderNumber(thisMonthOrderNumber);
        report.setThisMonthVisitsNumber(thisMonthVisitsNumber);
        //查询预约套餐总数量
        Integer count = hotSetmealDao.findAllBySetmeal();
        List<HotSetmeal> hotSetmeals = new ArrayList<>();

        //查询最热套餐的ID
        List<Integer> hotSetmealIds = hotSetmealDao.findSetmeal();

        for (Integer hotSetmealId : hotSetmealIds) {
            HotSetmeal hotSetmeal = new HotSetmeal();
            //根据预约套餐ID查询套餐名字
            String name = setMealDao.findById(hotSetmealId);
            //根据预约套餐ID查询套餐备注
            // String remark = setMealDao.findRemarkById(hotSetmealId);
            //根据最热套餐ID查询数量
            Integer countBySetmealId = hotSetmealDao.findCountBySetmealId(hotSetmealId);
                /*if (remark != null && remark.length() > 0) {
                    hotSetmeal.setRemark(remark);
                }
                //获取预约套餐数量*/
            int setmealCount = hotSetmealDao.findCountBySetmealId(hotSetmealId);
            Double proportion = setmealCount * 1.0 / count; //Double.valueOf(setmealCount / count);
            hotSetmeal.setName(name);
            //hotSetmeal.setSetmealId(hotSetmealId);
            hotSetmeal.setSetmeal_count(countBySetmealId);
            hotSetmeal.setProportion(proportion);
            hotSetmeals.add(hotSetmeal);
        }

        report.setHotSetmeal(hotSetmeals);
        return report;
    }

    //每月1号上午九点半触发一次查询
    @Scheduled(cron = "0 30 9 1 * ?")
    @Override
    public Map<String, Object> getMemberReport() {
        HashMap<String, Object> map = new HashMap<>();
        /*按月统计连续12个月

        连续12个月中，第一个月份应加上之前的所有会员数据
        最后一个月份应为当前月的所有会员数据与上个月的会员总数的和*/
        //获取之前12个月的月份
        String[] latest12Months = new String[12];
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) + 1); //要先+1,才能把本月的算进去
        for (int i = 0; i < 12; i++) {
            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1); //逐次往前推1个月
            latest12Months[11 - i] = cal.get(Calendar.YEAR) + "." + fillZero(cal.get(Calendar.MONTH) + 1);
        }
        List<String> months = Arrays.asList(latest12Months);
        map.put("months", months);

        //获取12个月的会员数量
        ArrayList<Integer> memberCount = new ArrayList<>();
        LocalDate today = LocalDate.now();
        for (long i = 11L; i >= 0L; i--) {
            LocalDate localDate = today.minusMonths(i);
            String ss = localDate.toString();
            Integer countByDate = reportDao.findCountByDate(ss);
            memberCount.add(countByDate);
        }
        map.put("memberCount", memberCount);
        return map;
    }

    @Override
    public Map<String, Object> getSetmealReport() {
        Map<String, Object> map = new HashMap<>();
        List<Setmeal> setmeals = setMealDao.findAll();
        List<String> setmealNames = new ArrayList<>();

        List<Map<String, Object>> setmealCount = new ArrayList<>();

        for (Setmeal setmeal : setmeals) {
            String name = setmeal.getName();
            setmealNames.add(name);
            Integer setmealId = setmeal.getId();
            Integer count = orderDao.findBySetmealId(setmealId);
            HashMap<String, Object> setmealCounts = new HashMap<>();

                setmealCounts.put("name", name);
                setmealCounts.put("value", count);
                setmealCount.add(setmealCounts);


        }

        map.put("setmealNames", setmealNames);
        map.put("setmealCount", setmealCount);
        return map;
    }


    /**
     * 格式化月份
     */
    private static String fillZero(int i) {
        String month = "";
        if (i < 10) {
            month = "0" + i;
        } else {
            month = String.valueOf(i);
        }
        return month;
    }

}

